<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>权限列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css">
    <script src="../layui/layui.js"></script>
    <style type="text/css">
        @media screen and (max-width: 768px) {
            .layui-elem-field legend {
                margin-left: 15px;
                padding: 0 7px;
                font-size: 0.9rem;
            }
            /*权限框*/
            .layui-transfer-box{
                width: 37.5%!important;
                height: 450px!important;
            }
            /*穿梭按钮*/
            .layui-transfer-active{
                width: 25%!important;
            }

            .layui-form-checkbox[lay-skin=primary] span{
                width: 50px;
                height: auto;
                font-size: 0.6rem!important;
                padding-right: 3px!important;
            }
            .layui-form-checkbox span{
                white-space: pre-wrap!important;
            }
            .layui-transfer-header{
                height: auto!important;
                padding-left: 0px!important;
            }
            .layui-transfer-data li{
                height: auto!important;
                padding: 3px 0px!important;
            }
            .layui-transfer-data{
                height: auto!important;
            }
            .layui-transfer-box .layui-form-checkbox{
                padding-left: 17px!important;
            }
            .layui-form-checkbox[lay-skin=primary] i{
                width: 12px;
                height: 12px;
                top: 3px;
            }
            .layui-transfer-active{
                margin: 0!important;
            }
        }
    </style>

</head>

<body>
<fieldset class="layui-elem-field layui-field-title">
    <legend>权限穿梭框</legend>
</fieldset>
<div id="test1" class="demo-"></div>
<input type="hidden" th:value="${positionId}" id="positionId"/>
<script th:inline=none>
    layui.use(['transfer', 'layer', 'util'], function(){
        var $ = layui.$
            ,transfer = layui.transfer
            ,layer = layui.layer
            ,util = layui.util;
        $.ajax({
            url: '/position/query?positionId='+$("#positionId").val(),
            type: 'post',
            success: function (res) {
                //定义标题及数据源
                transfer.render({
                    elem: '#test1'
                    ,title: ['未获得权限', '已获得权限']  //自定义标题
                    ,data: res.data.tdlist
                    ,value: res.data.positionhasrole
                    ,onchange: function(obj, index){
                        var needrole=[]
                        for(var  i=0;i<obj.length;i++){
                            needrole.push(obj[i].value)
                        }
                        console.log(needrole)
                        console.log(index)
                        if(index==0){
                            $.ajax({
                                url: '/position/addpositionrole?positionId='+$("#positionId").val() +"&roleList="+needrole,
                                type: 'post',
                                success: function (res) {
                                    console.log("增加成功")
                                }
                            });
                        }
                        if(index==1){
                            $.ajax({
                                url: '/position/deletepositionrole?positionId='+$("#positionId").val() +"&roleList="+needrole,
                                type: 'post',
                                success: function (res) {
                                    console.log("删除成功")
                                }
                            });
                        }
                    }
                })
            }, error: function () {
            }
        });

    });
</script>

</body>
</html>